package cn.wolfcode.crud.util;

import cn.wolfcode.crud.domain.Employee;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpSession;
import java.util.List;

public class UserContext {
    //定义两个常量值
    public static final String EMPLOYEE_IN_SESSION="EMPLOYEE_IN_SESSION";
    public static final String PERMISSION_IN_SESSION="EMPLOYEE_IN_SESSION";
    //提供一个拿到session的方法
    public static HttpSession getSession(){
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();

        return requestAttributes.getRequest().getSession();
    }
    //提供get和set方法
    public static Employee getUserContext(){
        return (Employee)getSession().getAttribute("EMPLOYEE_IN_SESSION");
    }
    public static void setUserContext(Employee employee){
        getSession().setAttribute("EMPLOYEE_IN_SESSION",employee);;
    }
    public static List<String> getPermissions(){
        return (List<String>)getSession().getAttribute("PERMISSION_IN_SESSION");
    }
    public static void setPermission(List<String> permission){
        getSession().setAttribute("PERMISSION_IN_SESSION",permission);;
    }
}
